﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Wedn.Net.Common;
using Wedn.Net.Model;

namespace Wedn.Net.Extensions
{
    /// <summary>
    /// 枚举器拓展方法类
    /// </summary>
    public static class IEnumerable
    {
        #region 生成标签枚举器链接集合 +static MvcHtmlString ToHtml(this IEnumerable<Term> source, UrlHelper url, string format, TermType termType = TermType.Default, string separator = "、")
        /// <summary>
        /// 生成标签枚举器链接集合
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="url">UrlHelper</param>
        /// <param name="format">
        /// 格式({0}:链接；{1}:名称)
        /// eg:"<a href=\"{0}\" target=\"_blank\" class=\"label label-important\">{1}<i class=\"label-arrow\"></i></a>"
        /// </param>
        /// <param name="termType"></param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static MvcHtmlString ToHtml(this IEnumerable<Term> source, UrlHelper url, string format, TermType termType = TermType.Default, string separator = "、")
        {
            string html = !source.Any()
                ? string.Empty
                : string.Join(separator, source.Select(c => string.Format(format, GetTermLink(url, termType, string.IsNullOrEmpty(c.Slug) ? c.Name : c.Slug), c.Name)));
            return new MvcHtmlString(html);
        }
        private static string GetTermLink(UrlHelper url, TermType termType, string item)
        {
            switch (termType)
            {
                case TermType.Default:
                    return url.Action("Tag", "Blog", new { item });
                case TermType.PostCategory:
                case TermType.LinkCategory:
                    return url.Action("Category", "Blog", new { item });
            }
            return string.Empty;
        } 
        #endregion
    }
}
